﻿using eProcurement.BLL;
using eProcurement.DAL;
using eProcurement.libs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace eProcurement.members
{
    public partial class NewProject : System.Web.UI.Page
    {
        protected void Page_Load( object sender, EventArgs e )
        {
            Manager manager = new Manager();

            if ( !manager.IsPurchaser( Context.User.Identity.Name ) )
            {
                Response.Redirect( "../Signup2.aspx" );
            }

            if ( !IsPostBack )
            {
                SetupCategories();
            }
        }

        private void SetupCategories()
        {
            ddlIndustryTag.DataSource = Manager.GetIndustryCategories( Server.MapPath( "../Xml/IndustryCategory.xml" ) );
            ddlIndustryTag.DataBind();
        }

        protected void btnSubmit_Click( object sender, EventArgs e )
        {
            Manager manager = new Manager();
            Project project = new Project();

            Purchaser purchaser = (Purchaser)manager.GetAppUser( Context.User.Identity.Name );

            project.PurchaserId = purchaser.Id;
            project.Title = txtProjectTitle.Value;
            project.CloseDate = Convert.ToDateTime( txtCloseDate.Value );
            project.Description = txtDescription.Value;
            project.IndustryCategory = ddlIndustryTag.Text;
            project.ScopeDocument = fuScope.FileBytes;
            project.AdditionalDocument = fuSupporting.FileBytes;
            project.AdditionalDocsName = fuSupporting.FileName;
            project.ScopeFileName = fuScope.FileName;
            project.State = ProjectState.OPEN.ToString();

            if ( project.CloseDate < DateTime.Today )
            {
                tipSaved.Visible = true;
                tipSaved.InnerText = "Error Saving: Date passed.";
            }
            else
            {
                if ( manager.AddProject( project ) )
                {
                    tipSaved.Visible = true;
                }
            }
        }
    }
}